package fr.alesia.capital.bean.simple;

import java.util.ArrayList;
import java.util.List;

import fr.alesia.capital.bean.Continent;
import fr.alesia.capital.bean.Partie;
import fr.alesia.capital.bean.Pays;
import fr.alesia.capital.bean.Reponse;
import fr.alesia.capital.factory.BeanFactory;

public class PartieSimple implements Partie{
	
	private int
		niveau,
		mode,
		question,
		phase
	;
	
	private List<Pays>
		pays,
		erreurs,
		valides
	;
	
	private List<Reponse> reponses;
	
	private boolean valide;
	
	private Continent continent;
	
	private long millis;
	
	private Pays item;
	
	public PartieSimple(List<Pays> pays, Continent continent, int niveau, int mode) {
		
		super();
		this.niveau = niveau;
		this.mode = mode;
		this.pays = pays;
		
		this.erreurs = new ArrayList<Pays>(pays.size());
		this.valides = new ArrayList<Pays>(pays.size());
		
		this.reponses = new ArrayList<Reponse>(pays.size());
		
		this.question = 0;
		this.phase = DEBUT;
		
		this.millis = 0;
		
		this.valide = false;
		
		this.continent = continent;
	}

	@Override
	public Continent getContinent() {
		return continent;
	}

	@Override
	public List<Pays> getErreurs() {
		return erreurs;
	}

	@Override
	public Long getMillis() {
		return millis;
	}

	@Override
	public Integer getMode() {
		return mode;
	}

	@Override
	public Integer getNiveau() {
		return niveau;
	}

	@Override
	public List<Pays> getPays() {
		return pays;
	}

	@Override
	public int getPhase() {
		return phase;
	}

	@Override
	public Integer getQuestion() {
		return question;
	}

	@Override
	public Integer getQuestions() {
		return pays.size();
	}

	@Override
	public List<Pays> getValides() {
		return valides;
	}

	@Override
	public boolean hasNext() {

		boolean hasNext = question < pays.size();
		
		switch (phase) {
		
			case DEBUT:
			case QUESTION:
				item = pays.get(question);
				question ++;
				break;
				
			default:
				break;
		}
		
		if(phase == DEBUT){
			phase = REPONSE;
		}
		else if(phase == QUESTION){
			phase = REPONSE;
		}
		else if(phase == REPONSE && hasNext){
			phase = QUESTION;
		}
		else{
			phase = FIN;
		}
		return(hasNext);
	}

	@Override
	public Pays next() {
		
		return item;
	}

	@Override
	public void remove() {
		
		throw new UnsupportedOperationException();		
	}

	@Override
	public void setMillis(long millis) {
		this.millis = millis;	
	}

	@Override
	public Pays getItem() {
		return item;
	}

	@Override
	public boolean isValide() {
		
		return valide;
	}

	@Override
	public void addErreur(String reponse) {
		
		this.valides.add(item);
		this.valide = false;
		this.reponses.add(BeanFactory.createReponse(item, valide, reponse));
	}

	@Override
	public void addValide(String reponse) {
		
		this.erreurs.add(item);
		this.valide = true;
		this.reponses.add(BeanFactory.createReponse(item, valide, reponse));
	}

	@Override
	public List<Reponse> getReponses() {
		return reponses;
	}
	
	
}
